﻿#include "api_server.h"

Server::Server(QObject *parent) : QObject(parent)
{

}

void Server::get_server_by_order_id(QJsonObject &data) {

}
void Server::get_server_by_order_id_finished() {

}

void Server::server_modify_info(QJsonObject &data) {

}
void Server::server_modify_info_finished() {

}

void Server::get_server_review_rank(QJsonObject &data) {

}
void Server::get_server_review_rank_finished() {

}

void Server::get_server_info(QString token, int uid) {
    this->jwt_token = token;
    this->uid = uid;

    QString url = "/server/get/server/info/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = this->uid;

    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &Server::get_server_info_finished);
}
void Server::get_server_info_finished() {
    QJsonObject tmp = this->request->get_return_data().toObject();
    this->id = tmp["id"].toInt();
    this->name = tmp["name"].toString();
    this->phone = tmp["phone"].toString();
    this->sex = tmp["sex"].toString();
//    qDebug() << this->name << endl;
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &Server::get_server_info_finished);
}

void  Server::get_all_unpaid_order() {
    QString url = "/order/get/all/unpaid/orders/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;

    this->order_request = new HttpPostRequest();
    this->order_request->post(url, jwt_token, data);

    QObject::connect(this->order_request, &HttpPostRequest::post_finished, this, &Server::get_all_unpaid_order_finished);
}
void  Server::get_all_unpaid_order_finished() {
    this->unpaid_order_list = this->order_request->get_return_data().toArray();
    emit this->get_all_unpaid_order_signal();
    QObject::disconnect(this->order_request, &HttpPostRequest::post_finished, this, &Server::get_all_unpaid_order_finished);
}

void  Server::check_out(int order_id, double total_money) {
    QString url = "/order/check/out/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["server_id"] = this->id;
    data["order_id"] = order_id;
    data["total_money"] = total_money;

    this->order_request = new HttpPostRequest();
    this->order_request->post(url, jwt_token, data);

    QObject::connect(this->order_request, &HttpPostRequest::post_finished, this, &Server::check_out_finished);
}
void  Server::check_out_finished() {
    emit this->check_out_signal();
    QObject::disconnect(this->order_request, &HttpPostRequest::post_finished, this, &Server::check_out_finished);
}
